package com.example.contactapp.providers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;

import com.example.contactapp.objects.ContactObject;
import com.example.contactapp.views.SectionListItem;

public class GetAllContact extends
		AsyncTask<Void, Void, ArrayList<SectionListItem>> {
	private Context ctx;
	private ProgressDialog pd;

	public GetAllContact(Context ctx) {
		this.ctx = ctx;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();

		pd = new ProgressDialog(ctx);
		pd.setCancelable(false);
		pd.setMessage("Loading contact...");
		pd.show();
	}

	@Override
	protected ArrayList<SectionListItem> doInBackground(Void... params) {
		ArrayList<SectionListItem> myArrayObject = new ArrayList<SectionListItem>();
		ContentResolver cr = ctx.getContentResolver();
		Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
				null, null, null);
		ContactObject object = null;
		if (cur.getCount() > 0) {
			while (cur.moveToNext()) {
				object = new ContactObject();
				int id = cur.getInt(cur
						.getColumnIndex(ContactsContract.Contacts._ID));
				object.setId(id);
				object.setName(cur.getString(cur
						.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
				
				// Gets the lookup key column index
				int mLookupKeyIndex = cur.getColumnIndex(Contacts.LOOKUP_KEY);
				// Gets the lookup key value
				String mCurrentLookupKey = cur.getString(mLookupKeyIndex);
				// Gets the _ID column index
				object.setContactUri(Contacts.getLookupUri(id,
						mCurrentLookupKey));
				
				
				if (Integer
						.parseInt(cur.getString(cur
								.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
					Cursor pCur = cr.query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ " = ?", new String[] { id + "" }, null);
					if (pCur.moveToNext()) {
						object.setAddress(pCur.getString(pCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
						object.setAvatar(pCur.getString(pCur
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI)));
					} else {
						object.setAddress("");
					}
					pCur.close();
				}

				myArrayObject.add(new SectionListItem(object));
			}
		}
		Collections.sort(myArrayObject, new Comparator<SectionListItem>() {

			@Override
			public int compare(SectionListItem lhs, SectionListItem rhs) {
				return lhs.toString().compareTo(rhs.toString());
			}

		});

		return myArrayObject;
	}

	@Override
	protected void onPostExecute(ArrayList<SectionListItem> result) {
		super.onPostExecute(result);
		pd.dismiss();
	}

}
